<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;

class Adminrole extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $data = Db::name('role')->select();
        return view('adminrole/index', ['data' => $data]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $data['one'] = Db::name('node')->where('pid', '0')->column('title', 'id');
        foreach ($data['one'] as $k => $v) {
            $data['two'][] = Db::name('node')->where('pid', $k)->column('title', 'id');
        }
        return view('adminrole/create', ['data' => $data]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $p = $request->post();
//        var_dump($p);
        // 处理数据
        $data_role = [
            'title' => $p['title'],
            'rname' => $p['rname'],
            'remark' => $p['remark'],
            'state' => 1
        ];
        $data_node = empty($p['node_id']) ? null : $p['node_id'];
        // var_dump($data);
        // 执行添加
        $result_role = Db::name("role")->data($data_role)->insert();
        $role_id = Db::name('role')->getLastInsID();
        if ($result_role > 0) {
            if ($data_node == null) {
                return $this->error('请选功能');
            } else {
                foreach ($data_node as $v) {
                    Db::name("role_node")->data(['role_id' => $role_id, 'node_id' => $v])->insert();
                }
                return $this->success('添加成功');
            }
        } else {
            return $this->error('添加失败');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $data = Db::name('role')->select($id);
        $data['one'] = Db::name('node')->where('pid', '0')->column('title', 'id');
        foreach ($data['one'] as $k => $v) {
            $data['two'][] = Db::name('node')->where('pid', $k)->column('title', 'id');
        }
        $result = Db::name('role_node')->where('role_id',$id)->select();
        foreach ($result as $v) {
            $res[] = $v['node_id'];
        }
        return view('adminrole/edit', ['data' => $data,'res'=>$res]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $p = $request->put();
//        var_dump($p);
        $data_role = [
            'title' => $p['title'],
            'rname' => $p['rname'],
            'remark' => $p['remark'],
            'state' => '1'
        ];

        $result = Db::name('role')->where('id', $id)->update($data_role);
        if ($request) {
            return $this->success('修改成功');
        } else {
            return $this->error('修改失败');
        }
        $data_node = empty($p['node_id']) ? null : $p['node_id'];

        if ($data_node == null) {
            return $this->error('请选择功能');
        } else {

        }
    }

    public function ajaxUpdate(Request $request)
    {
        $p = $request->post();
        $nodeid = $p['nodeid'];
        $roleid = $p['roleid'];
        $status = $p['status'];
        if ($status=='true') {
            Db::name('role_node')->insert(['node_id'=>$nodeid,'role_id'=>$roleid]);
            return json('添加成功');
        } else {
            Db::name('role_node')->where('role_id',$roleid)->where('node_id',$nodeid)->delete();
            return json('删除成功');
        }
    }


    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $result1 = Db::name('role')->delete($id);
        $resule2 = Db::name('role_node')->where('role_id=' . $id)->delete();
        if ($result1 > 0 && $resule2 > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的用户删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['msg'] = 'ID为: ' . $id . '的用户删除失败!';
        }
        return json($info);
    }
}
